package com.nts.pmchelperdemo3.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nts.pmchelperdemo3.dto.SchedulingDTO;
import com.nts.pmchelperdemo3.result.Result;
import com.nts.pmchelperdemo3.service.SchedulingService;
import com.nts.pmchelperdemo3.dto.*;
import com.nts.pmchelperdemo3.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 排班管理控制器（生成排班计划、查询排班详情等）
 */
@RestController
@RequestMapping("/api/schedulings")
@Api(tags = "排班管理接口")
public class SchedulingController {

    @Autowired
    private SchedulingService schedulingService;

    /**
     * 为指定订单生成排班计划（支持磨刀/涂层阶段）
     * 注：磨刀排班由系统自动触发，涂层排班需用户手动触发
     */
    @PostMapping("/order/{orderId}")
    @ApiOperation("生成订单的排班计划（磨刀/涂层）")
    public Result<List<SchedulingDTO>> generateScheduling(
            @PathVariable Long orderId,
            @RequestParam Integer taskType) {  // 0-磨刀，1-涂层
        List<SchedulingDTO> schedulings = schedulingService.generateScheduling(orderId, taskType);
        return ResultUtil.success(schedulings, "排班计划生成成功");
    }

    /**
     * 查询订单的排班详情
     */
    @GetMapping("/order/{orderId}")
    @ApiOperation("查询订单的排班详情")
    public Result<List<SchedulingDTO>> getSchedulingByOrder(@PathVariable Long orderId) {
        List<SchedulingDTO> schedulings = schedulingService.getSchedulingByOrder(orderId);
        return ResultUtil.success(schedulings, "订单排班详情查询成功");
    }

    /**
     * 查询机器的排班计划
     */
    @GetMapping("/machine/{machineId}")
    @ApiOperation("查询单台机器的排班计划")
    public Result<List<SchedulingDTO>> getSchedulingByMachine(
            @PathVariable Long machineId,
            @RequestParam(required = false) String date) {  // 可选：按日期筛选
        List<SchedulingDTO> schedulings = schedulingService.getSchedulingByMachine(machineId, date);
        return ResultUtil.success(schedulings, "机器排班计划查询成功");
    }

    /**
     * 手动调整排班计划（仅管理员可用）
     */
    @PutMapping("/{id}")
    @ApiOperation("手动调整排班计划（管理员功能）")
    public Result<SchedulingDTO> adjustScheduling(
            @PathVariable Long id,
            @RequestBody SchedulingDTO schedulingDTO) {
        SchedulingDTO adjusted = schedulingService.adjustScheduling(id, schedulingDTO);
        return ResultUtil.success(adjusted, "排班计划调整成功");
    }

    /**
     * 同步订单状态（根据当前时间自动更新）
     * 注：通常由定时任务触发，此处提供手动触发接口
     */
    @PostMapping("/sync-status")
    @ApiOperation("手动同步订单状态（根据排班时间）")
    public Result<Void> syncOrderStatus() {
        schedulingService.syncOrderStatusByTime();
        return ResultUtil.success(null, "订单状态同步完成");
    }

    @GetMapping
    @ApiOperation("查询机器最晚完成时间")
    public Result<List<MachineLatestScheduleDTO>> getAllSchedulings (){
        List<MachineLatestScheduleDTO> result = schedulingService.getAllSchedulingsWithEndTime();
        return ResultUtil.success(result,"查询成功");
    }

    @GetMapping("/detail")
    @ApiOperation("查询排班计划细节")
    public Result<IPage<SchedulingDetailDTO>> getSchedulingDetail(
            @RequestParam (defaultValue = "1") Integer pageNum,
            @RequestParam (defaultValue = "20") Integer pageSize) {
        Page<SchedulingDetailDTO> page = new Page<>(pageNum,pageSize);
        IPage<SchedulingDetailDTO> detailPage = schedulingService.getSchedulingDetail(page);
        return ResultUtil.success(detailPage,"排班详情查询成功");
    }
}
